<template>
<div class="login-container">
        <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片展示区 -->
    <el-card class="box-card">
        <!-- 搜索框 -->
        <el-row :gutter="20">
            <el-col :span="8">
                <div>
                    <el-input placeholder="请输入内容" v-model="usersInfo.query" clearable @clear="getUsersList()">
                        <el-button slot="append" icon="el-icon-search" @click="getUsersList()"></el-button>
                    </el-input>
                </div>
            </el-col>
            <el-col :span="6"><div><el-button type="primary" @click="dialogVisible = true">添加用户</el-button></div></el-col>
        </el-row>
        <!-- 用户列表展示区 -->
        <el-table :data="userslist" style="width: 100%" border>
            <el-table-column type="index" label="序号"></el-table-column>
            <el-table-column prop="username" label="姓名"></el-table-column>
            <el-table-column prop="email" label="邮箱"></el-table-column>
            <el-table-column prop="mobile" label="电话"></el-table-column>
            <el-table-column prop="role_name" label="角色"></el-table-column>
            <el-table-column prop="mg_state" label="状态">
                <template slot-scope="scope">
                <el-switch v-model="scope.row.mg_state" @change="userStateChang(scope.row)"></el-switch>
                </template>
            </el-table-column>
            <el-table-column prop="type" label="操作">
                <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
                <el-button type="danger" icon="el-icon-delete" size="mini"></el-button>
                <!-- 分配角色 -->
                <el-tooltip effect="dark" content="分配角色" placement="top" :enterable="false">
                <el-button type="warning" icon="el-icon-setting" size="mini"></el-button>
                </el-tooltip>
            </el-table-column>
        </el-table>
        <!-- 分页功能区 -->
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="usersInfo.pagenum"
        :page-sizes="[1, 2, 5, 8]" :page-size="usersInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
        <!-- 添加用户对话框 -->
        <el-dialog :visible.sync="dialogVisible" title="添加用户" width="30%">
        <span>
          <el-form :model="addForm" :rules="addrules" label-width="100px" ref="addFormRef">
            <el-form-item label="用户名" prop="username">
              <el-input v-model="addForm.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input v-model="addForm.password"></el-input>
            </el-form-item>
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="addForm.email"></el-input>
            </el-form-item>
            <el-form-item label="电话" prop="phone">
              <el-input v-model="addForm.phone"></el-input>
            </el-form-item>
          </el-form>
        </span>
        <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
        </span>
        </el-dialog>
    </el-card>
</div>
</template>

<script>
export default {
  data () {
    return {
      // 用户列表参数对象
      usersInfo: {
        query: '',
        pagenum: 1,
        pagesize: 2
      },
      userslist: [],
      total: 0,
      dialogVisible: false,
      addForm: {
        username: '',
        password: '',
        email: '',
        phone: ''
      },
      addrules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { min: 6, max: 22, message: '长度在 3 到 22 个字符', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入电话', trigger: 'blur' },
          { min: 8, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getUsersList()
  },
  methods: {
    async getUsersList () {
      const { data: res } = await this.$http.get('users', { params: this.usersInfo })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.userslist = res.data.users
      this.total = res.data.total
      console.log(res)
    },
    // 每页多少条
    handleSizeChange (val) {
      this.usersInfo.pagesize = val
      console.log(val)
      this.getUsersList()
    },
    // 当前页
    handleCurrentChange (val1) {
      this.usersInfo.pagenum = val1
      console.log(val1)
      this.getUsersList()
    },
    async userStateChang (usersInfo) {
      console.log(usersInfo)
      const { data: res } = await this.$http.put(`users/${usersInfo.id}/state/${usersInfo.mg_state}`)
      if (res.meta.status !== 200) {
        usersInfo.mg_state = !usersInfo.mg_state
        return this.$message.error('状态更新失败')
      }
      this.$message.success('状态更新成功')
    }
  }
}
</script>

<style lang="less" scoped>
.el-pagination,.el-table {
    margin-top: 10px;
}
</style>
